<template>
  <div>
    <!-- van-diver -->
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :error.sync='error' error-text="请求失败,请点击重试"
      :immediate-check="false" @load="onLoad">
      <!-- <van-cell v-for="(item,ind) in list" :key="ind" :title="item.content" /> -->
      <comment-item :comment='item' v-for="item in list" :key="item.com_id+''" @replyShow="$emit('replyShow',$event)" />
    </van-list>
  </div>
</template>

<script>
// 导入接口
import { getCommentList } from '@/api/comment.js'
import CommentItem from '@/components/comment-item'
export default {
  props: {
    // 评论类型
    type: {
      type: String,
      default: 'a',
      validator (val) {
        return val === 'a' || val === 'c'
      }
    },
    source: {
      type: [String, Number, Object],
      require: true
    },
    // 评论列表
    list: {
      type: Array,
      default: () => []
    }
  },
  name: 'article-comment',
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      limit: 10, // 评论个数
      offset: null,
      totalCount: 0,
      error: false
    }
  },
  methods: {
    // 评论列表
    async onLoad () {
      try {
        const { data: res } = await getCommentList({
          source: this.source + '',
          limit: this.limit,
          type: this.type,
          offset: this.offset
        })
        console.log(res)
        this.list.push(...res.data.results)
        this.totalCount = res.data.total_count
        // 传递总条数
        this.$emit('updaTotal', this.totalCount)
        // 加载状态
        this.loading = false

        // 判断是否加载完
        if (this.list.length === this.totalCount) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
      } catch (err) {
        console.log(err)
        this.loading = false
        this.error = true
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: { CommentItem },
  created () {
    this.onLoad()
  },
  beforeupdated () {
    this.onLoad()
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
</style>
